// Eric Lorenz, Matt Rodano, Thomas Bradley
// SSW-567, Spring 2010, Professor Laird
// Assignment : Triangle Program

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <cmath>
#include <conio.h>
#include <algorithm>
#include <sstream>
using namespace std;

double validateString(string str)
    {
        float length;
        char * pEnd;
        char c_str[10];
        string::size_type position1 = -9, position2 = -8;
         
// Check if user has used "sqrt(x)" in input field.  If so, extract the number to be used for the length of the side of the triangle
        position1 = str.find("sqrt(");
         
        if(position1 != string::npos)
        {
             position2 = str.find(")");
             str.copy(c_str, position2 - (position1 + 5), position1 + 5);
             
             length = strtod(c_str, &pEnd);
             length = sqrt(length);
        }   
// If no "sqrt(x)" in input, convert the string to a double for the length of the side of the triangle
        else
        { 
             strcpy (c_str, str.c_str());
             
             length = strtod(c_str, &pEnd);
        }

// Test if the number for the length of the side of the triangle is invalid or out of bounds and display appropriate error mesage.
// If not, return the number to the main line
        if (length == 0.0)
           cout<<"\nNot a valid number! ";
           
        if (errno == ERANGE)
        {
           cout<<"\nNumber out of range! ";
           length = -999.0;
        }
           
        if (length < 0.0)
           cout<<"\nPositive numbers only! ";
           
           
        if (length >= 1001)
        {
           cout<<"\nNumber must be less than 1001! ";
           length = -99.0;
        }
           
        return length;              
    }


int main()
{
// Delcarations
     string side_length;
     float s1 = -1.0, s2 = -1.0, s3 = -1.0, temp = 0.0, least_side = 0.0;
     
// Input
     while(s1 <= 0.0)
     {
          cout<<"Please enter the length of side 1: ";
          cin>>side_length;
     
          s1 = validateString(side_length);
          
          least_side = s1;
     }
          
     while(s2 <= 0.0)
     {
          cout<<"Please enter the length of side 2: ";
          cin>>side_length;
     
          s2 = validateString(side_length);
          
          if( s2 < least_side )
              least_side = s2;
     }
     
     while(s3 <= 0.0)
     {
          cout<<"Please enter the length of side 3: ";
          cin>>side_length;
     
         s3 = validateString(side_length);
         
          if( s3 < least_side )
              least_side = s3;
     }
  
//Output

     if( (s1 + s2 > s3) && (s1 + s3 > s2) && (s2 + s3) > s1 )
     {
   
          cout<<"\n\nThis is a";
    
          if(s1==s2 && s2==s3)
               cout<<"n Equilateral ";

          if( (s1==s2 && s2!=s3) || (s2==s3 && s3!=s1) || (s3==s1 && s1!=s2) )
               cout<<"n Isosceles ";
     
          if(s1!=s2 && s2!=s3 && s3!=s1)
               cout<<" Scalene ";
          
// Updated test for a right triangle.  
// If the difference between the sum of the squares of any two sides and the square of the third side
// is within +/- 2% of the length of the smallest side, it is a  right triangle
          if (abs((s1 * s1 + s2 * s2) - (s3 * s3)) <= (least_side * .02))
               cout<<"Right ";
          else if (abs((s1 * s1 + s3 * s3) - (s2 * s2)) <= (least_side * .02))
                    cout<<"Right ";
               else if (abs((s2 * s2 + s3 * s3) - (s1 * s1)) <= (least_side * .02))
                    cout<<"Right ";     
          
          cout<<"Triangle.";
     }    
     else

         cout<<"\nError: Not a valid triangle, sum of any two sides must be greater than the third side!\n"; 

     cout<<"\n\n";
     system("pause");
                    
     return 0;     
     
}
